{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9933071490757153\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "#一个输入，一个输出\n",
    "def sigmoid(z):\n",
    "    return 1/(1+pow(math.e,-z))\n",
    "class Network():\n",
    "    def __init__(self):\n",
    "        self.weight = 1\n",
    "        self.biase = 2\n",
    "    def y(self, x):\n",
    "        return sigmoid(self.weight*x+self.biase)\n",
    "net1 = Network()\n",
    "print(net1.y(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.33017342]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#n个输入 一个输出\n",
    "class Network2():\n",
    "    def __init__(self,n):\n",
    "        self.weight = np.random.randn(n, 1)\n",
    "        self.biase = 2\n",
    "    def y(self, x):\n",
    "        return sigmoid(np.matmul(x,self.weight)+self.biase)\n",
    "net2 = Network2(5)\n",
    "print(net2.y([1,2,3,4,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.38677738]\n",
      " [0.94773753]\n",
      " [0.99996748]]\n"
     ]
    }
   ],
   "source": [
    "# n输入m输出,一层神经元\n",
    "class Network3():\n",
    "    def __init__(self, n, m):\n",
    "        self.n = n\n",
    "        self.m = m\n",
    "        self.weight = np.random.randn(m, n)\n",
    "        self.biase = np.random.randn(m, 1)\n",
    "\n",
    "    def y(self, x):\n",
    "        return sigmoid(np.matmul(self.weight, x)+self.biase)\n",
    "net3 = Network3(6, 3)\n",
    "print(net3.y([[1],[2],[3],[4],[5],[6]]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.57785962]]\n"
     ]
    }
   ],
   "source": [
    "#双层神经元，一层输出作为另一层的输入\n",
    "#__call__()函数\n",
    "class Network4():\n",
    "    def __init__(self, n, m):\n",
    "        self.n = n\n",
    "        self.m = m\n",
    "        self.weight = np.random.randn(m, n)\n",
    "        self.biase = np.random.randn(m, 1)\n",
    "\n",
    "    def __call__(self, x):\n",
    "        return sigmoid(np.matmul(self.weight, x)+self.biase)\n",
    "net4 = Network4(6, 3)\n",
    "net5 = Network4(3, 1)\n",
    "print(net5(net4([[1], [2], [3], [4], [5],[6]])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.19327464]]]\n"
     ]
    }
   ],
   "source": [
    "#输入各层数神经元数目，输出一个结果\n",
    "class Network5():\n",
    "    def __init__(self,sizes):\n",
    "        self.sizes = sizes\n",
    "        self.w_lists=[]\n",
    "        self.b_lists=[]\n",
    "        for i in range(len(sizes)-1):\n",
    "            self.w_lists.append([np.random.randn(sizes[i+1],sizes[i])])\n",
    "            self.b_lists.append([np.random.randn(sizes[i+1],1)])\n",
    "    def __call__(self, x):\n",
    "        for w , b in zip(self.w_lists,self.b_lists) :\n",
    "            x=sigmoid(np.matmul(w, x)+b)\n",
    "        return x\n",
    "net6 = Network5([6,3,2,1])\n",
    "print(net6([[1], [2], [3], [4], [5],[6]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
